package com.bootdo.wx.service.impl;

import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.wx.dao.SalesCouponDao;
import com.bootdo.wx.domain.SalesCouponDO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import com.bootdo.wx.dao.MemberCouponDao;
import com.bootdo.wx.domain.MemberCouponDO;
import com.bootdo.wx.service.MemberCouponService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
public class MemberCouponServiceImpl implements MemberCouponService {
	@Autowired
	private MemberCouponDao memberCouponDao;
	@Autowired
	private SalesCouponDao salesCouponDao;
	
	@Override
	public MemberCouponDO get(String id){
		return memberCouponDao.get(id);
	}
	
	@Override
	public List<MemberCouponDO> list(Map<String, Object> map){
		return memberCouponDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberCouponDao.count(map);
	}
	
	@Override
	public int save(MemberCouponDO memberCoupon){
		return memberCouponDao.save(memberCoupon);
	}
	
	@Override
	public int update(MemberCouponDO memberCoupon){
		return memberCouponDao.update(memberCoupon);
	}
	
	@Override
	public int remove(String id){
		return memberCouponDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberCouponDao.batchRemove(ids);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public List<Map<String,Object>> getNewCoupon(Map<String, Object> map) {
		List<Map<String,Object>> newList = new ArrayList<Map<String,Object>>();
		map.put("hasScore","1");
		//得到现在可以用的优惠券
		List<SalesCouponDO> list = salesCouponDao.getCanList(map);
		String userId = map.get("userId").toString();
		BigDecimal allPrice = new BigDecimal(0);
		for (SalesCouponDO salesCouponDO : list) {
			String couponId = salesCouponDO.getId();
			int limitNumber = salesCouponDO.getLimitNumber();  //优惠券单人最多领取个数
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("userId",userId);
			map1.put("couponId", couponId);
			int size = memberCouponDao.list(map1).size();   //自己已经领取个数
			int haveNum = salesCouponDO.getTotalNum() - salesCouponDO.getSendNum();  //优惠券余量
			int canNum = 0;   //自己还可以领取数量
			if (haveNum > limitNumber) {
				canNum = limitNumber-size;
			} else {
				canNum = haveNum-size;
			}
			if (canNum > 0) {
				Map<String,Object> newMap = new HashMap<String,Object>();
				newMap.put("salesId",salesCouponDO.getId());
				newMap.put("name",salesCouponDO.getName());
				newMap.put("money",salesCouponDO.getMoney());
				newMap.put("enoughMoney",salesCouponDO.getEnoughMoney());
				newMap.put("limit_endAt",salesCouponDO.getLimitEndat());
				newList.add(newMap);
			}
		}
		return newList;

	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public PageInfo<Map<String,Object>> getMyCouponList(Map<String, Object> map) {
		//更新自己过期优惠券
		memberCouponDao.updateCouponByTime(map);
		PageHelperUtils.page(map);
		String type = map.get("type").toString();
		List<Integer> types = new ArrayList<Integer>();
		if (type.equals("0")) {
			types.add(0);
		} else if (type.equals("1")) {
			types.add(1);
		} else if (type.equals("2")) {
			types.add(2);
			types.add(3);
		}
		map.put("types", types);
		List<Map<String,Object>> list = memberCouponDao.getMyCouponList(map);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public R saveCoupon(Map<String, Object> paramMap) {
		String userId = paramMap.get("userId").toString();
		String couponId = paramMap.get("couponId").toString();
		Date date = new Date();
		SalesCouponDO salesCouponDO = salesCouponDao.get(couponId);
		//插入会员优惠券表
		MemberCouponDO memberCouponDO = new MemberCouponDO();
		memberCouponDO.setId(UuidUtil.get32UUID());
		memberCouponDO.setDelFlag(0);
		memberCouponDO.setCouponId(couponId);
		memberCouponDO.setType(1);
		memberCouponDO.setCouponName(salesCouponDO.getName());
		memberCouponDO.setCouponMoney(salesCouponDO.getEnoughMoney());
		memberCouponDO.setMoney(salesCouponDO.getMoney());
		memberCouponDO.setStatus(0);
		memberCouponDO.setCreateAt(date);
		memberCouponDO.setUpdateAt(date);
		memberCouponDO.setUserId(userId);
		memberCouponDao.save(memberCouponDO);
		//修改优惠券表
		SalesCouponDO couponDO = new SalesCouponDO();
		couponDO.setId(salesCouponDO.getId());
		couponDO.setSendNum(salesCouponDO.getSendNum() + 1);
		salesCouponDao.update(couponDO);
		return R.ok();
	}


	
}
